<?php
class updateopt extends control
{
    public function __construct()
    {
        parent::__construct();
        $this->app->loadLang('index');
       /// $this->_network=$this->app->loadClass('network');
        if(!isset($_SESSION['username'])){
          $this->locate($this->createLink('index','login'));
        }
        $this->sysmanage=$this->loadModel('sysmanage');
    }
    /*********************************************************************************/
    /**
     * 检测站点是否能访问
     */
    public function test_sites(){
    	//set_time_limit(0);//不做超时设置
    	$where=new stdClass();
    	$where->status='all';
    	$sites_arr=$this->updateopt->get_sites_info(null,$where);
    	//var_dump($sites_arr);
    	foreach($sites_arr as $val){
    		$ipv4="$val->visit_domain";
    		$ipv6="$val->pre_v6.$val->main_domain";
    		$data_ipv4=$this->updateopt->test_url($ipv4,'ipv4');
    		$data_ipv6=$this->updateopt->test_url($ipv6,'ipv6');
    		$data=new stdClass();
    		$data->source_status=$data_ipv4['ipv4_status'];
    		$data->ipv6_status=$data_ipv6['ipv6_status'];
    		$this->updateopt->update_sites_info($data,$val->sites_id);
    	}
    	echo 'success';
    }
    
    /**
     * 写入数据到文件里面
     */
    public function write_sites(){
    	$file_path=$this->config->apacheConfigPath;
    	$write_str="";
    	//得到基础配置
    	$sites_basicconf=$this->updateopt->get_basicconf_info();
    	
    	if($sites_basicconf->ProxyRequests!='none'){
    		$write_str.="ProxyRequests ".$sites_basicconf->ProxyRequests."\r\n";
    	}
    	if($sites_basicconf->ProxyVia!='none'){
    		$write_str.="ProxyVia ".$sites_basicconf->ProxyVia."\r\n";
    	}
   		if($sites_basicconf->ProxyPreserveHost!='none'){
    		$write_str.="ProxyPreserveHost ".$sites_basicconf->ProxyPreserveHost."\r\n";
    	}
    	if($sites_basicconf->UseCanonicalName!='none'){
    		$write_str.="UseCanonicalName ".$sites_basicconf->UseCanonicalName."\r\n";
    	}
    	if($sites_basicconf->ProxyMaxForwards!='none'){
    		$write_str.="ProxyMaxForwards ".$sites_basicconf->ProxyMaxForwards."\r\n";
    	}
    	
    	//得到站点数据
    	$where=new stdClass();
    	$where->status=1;
    	$sites_arr=$this->updateopt->get_sites_info(null,$where);
    	foreach($sites_arr as $val){
    		$write_str.=$this->get_one_site_info($val);
    	}
    	//写入文件数据
    	$f=fopen($file_path, "wb");  
    	fputs($f, $write_str);  
		fclose($f);
		shell_exec($this->config->apacheConfigOpt);
    	echo 'success';
    }
    /**
     * 根据一行记录得到一站点数据
     */
    public function get_one_site_info($data){
    	$return_str="";
    	$return_str.="\r\n<VirtualHost *:80>\r\n";
    	$return_str.="ServerAlias $data->pre_v6.$data->main_domain\r\n";
		$return_str.="<Proxy>\r\n";
		$return_str.="Order deny,allow\r\n";
		$return_str.="Allow from all\r\n";
		$return_str.="</Proxy>\r\n";
		$return_str.="ProxyPass / $data->visit_type://$data->visit_domain/\r\n";
		$return_str.="ProxyPassReverse / $data->visit_type://$data->visit_domain/\r\n";
		$return_str.="ProxyPassReverseCookieDomain $data->visit_type://$data->pre_v6.$data->main_domain/  $data->visit_type://$data->visit_domain/\r\n";
		$return_str.="</VirtualHost>\r\n";
		return $return_str;
    }
    
    //升级操作
    public function updateopt($recTotal = 0, $recPerPage = 20, $pageID = 0,$key_word='',$status='all')
    {
		
    	$this->view->cssload =array('theme/default/css/report_alarm_fault.css',
    	                             'theme/default/css/style/cupertino/jquery-ui-1.7.2.custom.css');
    	$this->view->jsload=array('scripts/jquery-ui-1.7.2.custom.min.js',
    	                          'scripts/timepicker.js',
    	                           'boot-popover.js' 
    	                           );  
		$this->view->title    = '升级操作_IPv6网站升级';
    	$this->app->loadClass('pager');
        $pager = new pager($recTotal, $recPerPage, $pageID);
        
        $where=new stdClass();
        $where->key_word=$key_word;
        $where->status=$status;
        $this->view->sites_info_list = $this->updateopt->get_sites_info($pager,$where);
        $this->view->pager    = $pager;
    	$this->display();
    }
 /**
     * 显示站点
     */
    public function add_sites(){
    	//得到所有网站类型
    	$this->view->type_info=$this->updateopt->get_type_info();
    	//var_dump($this->updateopt->get_type_info());
    	//得到所有网站区域
    	$where=new stdClass();
    	$where->reid=0;
    	$this->view->area_info=$this->updateopt->get_area_info(null,$where);
    	//var_dump($this->updateopt->get_area_info());
    	//得到所有单位
    	$this->view->unit_info=$this->updateopt->get_unit_info();
    	//var_dump($this->updateopt->get_unit_info());
    	
    	if(isset($_POST['sites_id'])&&!empty($_POST['sites_id'])){//编辑时得到信息
    		$this->view->sites_info=$this->updateopt->get_sites_info_by_id($_POST['sites_id']);
    	}
    	$this->display();
    }
	/**
     * 处理站点
     */
    public function done_add_sites(){
        if(!empty($_POST))
        {
        	if(!empty($_POST['sites_id'])){//修改
        		$this->sysmanage->set_optlog('修改站点'.$_POST['sites_name']);
	        	$data=new stdClass();
    			$data->sites_name=$_POST['sites_name'];
    			$data->type_id=$_POST['type_id'];
    			$data->area_id=$_POST['area_id'];
    			$data->area_id2=$_POST['area_id2'];
    			$data->unit_id=$_POST['unit_id'];
    			$data->main_domain=$_POST['main_domain'];
    			$data->visit_domain=$_POST['visit_domain'];
    			$data->visit_type=$_POST['visit_type'];
    			$data->pre_v6=$_POST['pre_v6'];
    			$data->sites_remark=$_POST['sites_remark'];
    			$data->status=$_POST['status'];
	            $id = $this->updateopt->update_sites_info($data,$_POST['sites_id']);
	            if(dao::isError()){
	               echo 'failure';
	            }else{
	               echo 'success';
	            }
        	}else{
        		$this->sysmanage->set_optlog('添加站点'.$_POST['sites_name']);
	        	$id = $this->updateopt->create_sites_info();
	            if(dao::isError()||$id==false){
	               echo 'failure';
	            }else{
	               echo 'success';
	            }
        	}
            
            
        }
    }
/**
      * 删除sites信息
      */
     public function del_sites_info(){
     	$this->sysmanage->set_optlog('删除站点信息'.$_POST['sites_id']);
        if(!empty($_POST['sites_id']))
        {
            $id = $this->updateopt->delete_sites_info($_POST['sites_id']);
            if(dao::isError()){
               echo 'failure';
            }else{
               echo 'success';
            }
        }
     }
     /**
      * 根据reid得到areainfo信息
      */
     public function get_area2_info(){
     	if(!empty($_POST['rid']))
        {
     		$where=new stdClass();
    		$where->reid=$_POST['rid'];
    		$area_info=$this->updateopt->get_area_info(null,$where);
    		echo json_encode($area_info);
        }
     }
   /**************************************************************************************************/ 
  /**
     * 基础服务配置
     * @access public
     * @return void
     */
    public function basicconf()
    {
    	//动态加载css
    	$this->view->cssload =array('theme/default/css/report_alarm_fault.css',
    	                             'theme/default/css/style/cupertino/jquery-ui-1.7.2.custom.css');
    	$this->view->jsload=array('scripts/jquery-ui-1.7.2.custom.min.js');
		$this->view->title    = '基础服务配置_IPv6网站升级';
		
		//var_dump($this->updateopt->get_basicconf_info());
        $this->view->basicconf_info= $this->updateopt->get_basicconf_info();
        
        $this->display();
    }
    
    /**
     * 异步更新基础配置数据
     */
    public function update_basicconf(){
    	$this->sysmanage->set_optlog('更新基础服务配置');
        if(!empty($_POST))
        {
        	$data=new stdClass();
	        /*$data->ProxyRequests=$_POST['ProxyRequests'];
    		$data->ProxyVia=$_POST['ProxyVia'];
    		$data->ProxyPreserveHost=$_POST['ProxyPreserveHost'];
    		$data->UseCanonicalName=$_POST['UseCanonicalName'];*/
    		$data->ProxyMaxForwards=$_POST['ProxyMaxForwards'];
	        $id = $this->updateopt->update_basicconf_info($data);
	        if(dao::isError()){
	            echo 'failure';
	        }else{
	            echo 'success';
	        }
        }
    }
    /****************************************************************************************************/
    //区域配置,省级
 	public function areaconf($recTotal = 0, $recPerPage = 20, $pageID = 0,$key_word=''){
    	$this->view->title    = '区域管理_IPv6网站升级';
    	//动态加载css
    	$this->view->cssload =array('theme/default/css/report_alarm_fault.css',
    	                             'theme/default/css/style/cupertino/jquery-ui-1.7.2.custom.css');
    	$this->view->jsload=array('scripts/jquery-ui-1.7.2.custom.min.js',
    	                          'scripts/timepicker.js',
    	                           'boot-popover.js' 
    	                           );                   
        $this->app->loadClass('pager');
        $pager = new pager($recTotal, $recPerPage, $pageID);
        
        $where=new stdClass();
        $where->key_word=$key_word;
        $where->reid=0;
        $this->view->area_info_list = $this->updateopt->get_area_info($pager,$where);
        $this->view->pager    = $pager;
    	$this->display();
    }
    /**
     * 显示区域
     */
    public function add_area(){
    	if(isset($_POST['area_id'])&&!empty($_POST['area_id'])){//编辑时得到信息
    		$this->view->area_info=$this->updateopt->get_area_info_by_id($_POST['area_id']);
    	}
    	$this->display();
    }
	/**
     * 处理区域
     */
    public function done_add_area(){
    	$this->sysmanage->set_optlog('添加区域'.$_POST['area_name']);
        if(!empty($_POST))
        {
        	if(!empty($_POST['area_id'])){//修改
	        	$data=new stdClass();
	        	$data->area_name=$_POST['area_name'];
    			$data->area_remark=$_POST['area_remark'];
	            $id = $this->updateopt->update_area_info($data,$_POST['area_id']);
	            if(dao::isError()){
	               echo 'failure';
	            }else{
	               echo 'success';
	            }
        	}else{
	        	$id = $this->updateopt->create_area_info();
	            if(dao::isError()||$id==false){
	               echo 'failure';
	            }else{
	               echo 'success';
	            }
        	}
            
            
        }
    }
/**
      * 删除area信息
      */
     public function del_area_info(){
     	$this->sysmanage->set_optlog('删除区域信息'.$_POST['area_id']);
        if(!empty($_POST['area_id']))
        {
            $id = $this->updateopt->delete_area_info($_POST['area_id']);
            if(dao::isError()){
               echo 'failure';
            }else{
               echo 'success';
            }
        }
     }
     /**
      * 显示地区信息
      */
     public function show_area(){
     	if(!empty($_POST['area_id']))
        {
        	$where=new stdClass();
        	$where->reid=$_POST['area_id'];
       		$this->view->area_info_list = $this->updateopt->get_area_info(null,$where);
        }
     	$this->display();
     }
    /*********************************************************************/
   //网站类型配置
 	public function typeconf($recTotal = 0, $recPerPage = 20, $pageID = 0,$key_word=''){
    	$this->view->title    = '网站类型管理_IPv6网站升级';
    	//动态加载css
    	$this->view->cssload =array('theme/default/css/report_alarm_fault.css',
    	                             'theme/default/css/style/cupertino/jquery-ui-1.7.2.custom.css');
    	$this->view->jsload=array('scripts/jquery-ui-1.7.2.custom.min.js',
    	                          'scripts/timepicker.js',
    	                           'boot-popover.js' 
    	                           );                   
        $this->app->loadClass('pager');
        $pager = new pager($recTotal, $recPerPage, $pageID);
        
        $where=new stdClass();
        $where->key_word=$key_word;
        $this->view->type_info_list = $this->updateopt->get_type_info($pager,$where);
        $this->view->pager    = $pager;
    	$this->display();
    }
    /**
     * 显示网站类型
     */
    public function add_type(){
    	if(isset($_POST['type_id'])&&!empty($_POST['type_id'])){//编辑时得到信息
    		$this->view->type_info=$this->updateopt->get_type_info_by_id($_POST['type_id']);
    	}
    	$this->display();
    }
	/**
     * 处理网站类型
     */
    public function done_add_type(){
    	$this->sysmanage->set_optlog('添加网站类型'.$_POST['type_name']);
        if(!empty($_POST))
        {
        	if(!empty($_POST['type_id'])){//修改
	        	$data=new stdClass();
	        	$data->type_name=$_POST['type_name'];
    			$data->type_remark=$_POST['type_remark'];
	            $id = $this->updateopt->update_type_info($data,$_POST['type_id']);
	            if(dao::isError()){
	               echo 'failure';
	            }else{
	               echo 'success';
	            }
        	}else{
	        	$id = $this->updateopt->create_type_info();
	            if(dao::isError()||$id==false){
	               echo 'failure';
	            }else{
	               echo 'success';
	            }
        	}
            
            
        }
    }
/**
      * 删除type信息
      */
     public function del_type_info(){
     	$this->sysmanage->set_optlog('删除网站类型信息'.$_POST['type_id']);
        if(!empty($_POST['type_id']))
        {
            $id = $this->updateopt->delete_type_info($_POST['type_id']);
            if(dao::isError()){
               echo 'failure';
            }else{
               echo 'success';
            }
        }
     }
     
 /**************************************************************************************************/
     
    //单位管理
    public function unitconf($recTotal = 0, $recPerPage = 20, $pageID = 0,$datetime_start='',$datetime_end='',$key_word=''){
    	$this->view->title    = '单位管理_IPv6网站升级';
    	//动态加载css
    	$this->view->cssload =array('theme/default/css/report_alarm_fault.css',
    	                             'theme/default/css/style/cupertino/jquery-ui-1.7.2.custom.css');
    	$this->view->jsload=array('scripts/jquery-ui-1.7.2.custom.min.js',
    	                          'scripts/timepicker.js',
    	                           'boot-popover.js' 
    	                           );

    	$pageJS .= <<<EOT
			var datatime_display = false;
			var settings = {
			    duration: '',
			    showTime: true,
			    constrainInput: false,
			    time24h: true,
			    closeText: '确定',
			    prevText: '上个月',
			    nextText: '下个月',
			    currentText: '今天',
			    dateFormat: 'yy-mm-dd',
			    firstDay: 1,
			    dayNamesMin: ['天', '一', '二', '三', '四', '五', '六'],
			    monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月', ]
			}
			$(function() {
			    $('#datetime_start').datepicker(settings);
			    $('#datetime_end').datepicker(settings);
			});
EOT;
    	$this->view->pageJS=$pageJS;
    	                      
        $this->app->loadClass('pager');
        $pager = new pager($recTotal, $recPerPage, $pageID);
        
        $where=new stdClass();
        $where->datetime_start=$datetime_start;
        $where->datetime_end=$datetime_end;
        $where->key_word=$key_word;
        
        $this->view->unit_info_list = $this->updateopt->get_unit_info($pager,$where);
        $this->view->pager    = $pager;
    	$this->display();
    }
    /**
     * 显示添加单位
     */
    public function add_unit(){
    	if(isset($_POST['unit_id'])&&!empty($_POST['unit_id'])){//编辑时得到信息
    		$this->view->unit_info=$this->updateopt->get_unit_info_by_id($_POST['unit_id']);
    	}
    	$this->display();
    }
 /**
     * 显示单位信息
     */
    public function show_unit(){
    	if(isset($_POST['unit_id'])&&!empty($_POST['unit_id'])){//编辑时得到信息
    		$this->view->unit_info=$this->updateopt->get_unit_info_by_id($_POST['unit_id']);
    	}
    	$this->display();
    }
	/**
     * 处理添加单位信息
     */
    public function done_add_unit(){
    	$this->sysmanage->set_optlog('添加单位'.$_POST['unit_name']);
        if(!empty($_POST))
        {
        	if(!empty($_POST['unit_id'])){//修改
	        	$data=new stdClass();
	        	$data->unit_name=$_POST['unit_name'];
    			$data->unit_remark=$_POST['unit_remark'];
    			$data->unit_linkman=$_POST['unit_linkman'];
    			$data->unit_contact=$_POST['unit_contact'];
	            $id = $this->updateopt->update_unit_info($data,$_POST['unit_id']);
	            if(dao::isError()){
	               echo 'failure';
	            }else{
	               echo 'success';
	            }
        	}else{
	        	$id = $this->updateopt->create_unit_info();
	            if(dao::isError()||$id==false){
	               echo 'failure';
	            }else{
	               echo 'success';
	            }
        	}
            
            
        }
    }
/**
      * 删除unit信息
      */
     public function del_unit_info(){
     	$this->sysmanage->set_optlog('删除单位信息'.$_POST['unit_id']);
        if(!empty($_POST['unit_id']))
        {
            $id = $this->updateopt->delete_unit_info($_POST['unit_id']);
            if(dao::isError()){
               echo 'failure';
            }else{
               echo 'success';
            }
        }
     }

}
